//
//  SJUserLoginView.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/9/30.
//  Copyright © 2015年 world. All rights reserved.
//
//  用户未登陆是看到的界面

import UIKit

///定义一份协议
protocol SJUserLoginViewDelegate:NSObjectProtocol{

    //注册按钮被点击
    func userLoginViewRegisterBtnDidClick()
    //登陆按钮被点击
    func userLoginViewLoginBtnDidClick()
}

class SJUserLoginView: UIView {

    ///声明代理属性
    weak var delegate:SJUserLoginViewDelegate?
    
    ///设置视图内容
    func setupViewContent(iconName: String,message: String,isHome:Bool){
    
        //设置标题文本
        messageLabel.text = message
        
        if isHome { //首页
            
            homeImageView.hidden = false
            homeImageView.image = UIImage(named: "visitordiscover_feed_image_house")
        }else{
        
            homeImageView.hidden = true
            iconImageView.image = UIImage(named: iconName)
            //将遮罩放到最底层
            sendSubviewToBack(maskIconView)
        }
    }

    ///开始动画
    func startAnimating(){
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = M_PI * 2
        ani.duration = 20
        ani.repeatCount =  MAXFLOAT
        
        iconImageView.layer.addAnimation(ani, forKey: nil)
    }
    
    override init(frame: CGRect) {
       super.init(frame: frame)
        //添加子控件
       setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -创建控件
    private func setupUI(){
        addSubview(iconImageView)
        addSubview(maskIconView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //布局子控件
        //addConstraint一般用来添加相对其他控件的约束
        //要添加约束先关闭autoresizing
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        //参数说明:要添加约束的控件 约束位置  与参照控件关系 参照控件 参照控件位置 乘以 偏移量
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        homeImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: homeImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: homeImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 40))
        
        //addConstraints 一般用来添加VFL语句  用来约束自己的宽与高
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":messageLabel]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":messageLabel]))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":registerButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":registerButton]))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":loginButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":loginButton]))
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]-160-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":maskIconView]))
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
    }
    
    // MARK: -懒加载控件
    ///圆环图标
    lazy var iconImageView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    ///遮罩图片
    lazy var maskIconView:UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)

    }()
    
    ///home图片
    lazy var homeImageView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    ///提示消息
    lazy var messageLabel: UILabel = {
        let label = UILabel(frame: CGRectMake(0, 64, 240, 40))
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.text = "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么"
        label.preferredMaxLayoutWidth = 224
        label.numberOfLines = 2
        label.sizeToFit()
        return label
    }()
    ///注册按钮
    lazy var registerButton: UIButton = {
       let btn = UIButton(frame: CGRectMake(0, 64, 100, 40))
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加点击事件
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    ///登陆按钮
    lazy var loginButton: UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 64, 100, 40))
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加事件
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    /// MARK - 按钮点击事件
    func register(){
        delegate?.userLoginViewRegisterBtnDidClick()
    }
    func login(){
        delegate?.userLoginViewLoginBtnDidClick()
    }
}
